package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.StaffManageBean;
import com.shqcjd.yunweiyanoperator.ui.adapter.StaffReviewAdapter;
import com.shqcjd.yunweiyanoperator.ui.adapter.StaffReviewAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.Config;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.WrapContentLinearLayoutManager;
import com.shqcjd.yunweiyanoperator.widget.AddStaffDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 员工审批
 */
public class StaffReviewActivity extends BaseActivity {

    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_review_manage)
    RecyclerView rvReviewManage;
    @BindView(R.id.sr_review_manage)
    SwipeRefreshLayout srReviewManage;
    @BindView(R.id.rl_isnull)
    RelativeLayout rlIsnull;

    private Context mContext;
    private int page = 1;
    private int isRefreshOrLoadMore = 0;//0代表刷新  1代表加载更多
    private StaffReviewAdapter staffReviewAdapter;
    private int pageSize;
    private final int PAGE_TOTAL = Config.PAGE_TOTAL;
    List<StaffManageBean.ListBean> mList = new ArrayList<>();

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_staff_review;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle.setText("员工审批");
        srReviewManage.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    private void refreshData() {
        refreshOrLoadMore();
        srReviewManage.setRefreshing(true);
        isRefreshOrLoadMore = 0;
        page = 1;
        mList.clear();
        getDataList(page);
    }

    private void loadMoreData() {
        isRefreshOrLoadMore = 1;
        getDataList(page + 1);
    }

    private void refreshOrLoadMore() {
        if (staffReviewAdapter != null) {
            staffReviewAdapter.notifyLoadMoreToLoading();
        }
    }


    private void getDataList(int p) {
        Map<String, String> map = new HashMap<>();
        map.put("page", p + "");
        map.put("size", PAGE_TOTAL + "");
        map.put("review", 0+"");//0:未审核,1:已审核,2:审核拒绝  不传就是所有
        ApiService.staffManage(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<StaffManageBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<StaffManageBean> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }
                        mList.addAll(responseData.data.list);
                        pageSize = responseData.data.list.size();
                        if (staffReviewAdapter == null) {
                            rvReviewManage.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
                            staffReviewAdapter = new StaffReviewAdapter(mList, mContext);
                            rvReviewManage.setAdapter(staffReviewAdapter);
                            staffReviewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent it = new Intent(mContext, UserInfoActivity.class);
                                    it.putExtra("name", mList.get(position).userName);
                                    it.putExtra("companyName", mList.get(position).businessName);
                                    it.putExtra("phone", mList.get(position).phoneNumber);
                                    it.putExtra("maintainInfo", mList.get(position).info);
                                    it.putExtra("isReview", 1);//员工详情页传0  员工管理页传1
                                    it.putExtra("employeeId", mList.get(position).id);
                                    startActivity(it);
                                }
                            });
                            staffReviewAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                @Override
                                public void onLoadMoreRequested() {
                                    rvReviewManage.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (PAGE_TOTAL > pageSize) {
                                                rvReviewManage.post(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        staffReviewAdapter.loadMoreEnd();
                                                    }
                                                });
                                            } else {
                                                loadMoreData();
                                            }
                                        }
                                    });
                                }
                            });
                        }
                        if (isRefreshOrLoadMore == 1) {
                            page = page + 1;
                            staffReviewAdapter.loadMoreComplete();
                        }
                        if (mList.size() == 0) {
                            rlIsnull.setVisibility(View.VISIBLE);
                        } else {
                            rlIsnull.setVisibility(View.INVISIBLE);
                        }
                        staffReviewAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        srReviewManage.setRefreshing(false);
                    }
                });
    }

    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }
}
